{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting redis\n",
      "  Downloading redis-5.0.4-py3-none-any.whl.metadata (9.3 kB)\n",
      "Requirement already satisfied: async-timeout>=4.0.3 in d:\\dev\\miniconda3\\envs\\python-study\\lib\\site-packages (from redis) (4.0.3)\n",
      "Downloading redis-5.0.4-py3-none-any.whl (251 kB)\n",
      "   ---------------------------------------- 0.0/252.0 kB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/252.0 kB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/252.0 kB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/252.0 kB ? eta -:--:--\n",
      "   ---- ----------------------------------- 30.7/252.0 kB ? eta -:--:--\n",
      "   ------ -------------------------------- 41.0/252.0 kB 393.8 kB/s eta 0:00:01\n",
      "   ------ -------------------------------- 41.0/252.0 kB 393.8 kB/s eta 0:00:01\n",
      "   ------ -------------------------------- 41.0/252.0 kB 393.8 kB/s eta 0:00:01\n",
      "   ----------- --------------------------- 71.7/252.0 kB 302.7 kB/s eta 0:00:01\n",
      "   ---------------- --------------------- 112.6/252.0 kB 409.6 kB/s eta 0:00:01\n",
      "   --------------------- ---------------- 143.4/252.0 kB 500.5 kB/s eta 0:00:01\n",
      "   --------------------- ---------------- 143.4/252.0 kB 500.5 kB/s eta 0:00:01\n",
      "   --------------------- ---------------- 143.4/252.0 kB 500.5 kB/s eta 0:00:01\n",
      "   -------------------------------------  245.8/252.0 kB 537.7 kB/s eta 0:00:01\n",
      "   -------------------------------------- 252.0/252.0 kB 533.6 kB/s eta 0:00:00\n",
      "Installing collected packages: redis\n",
      "Successfully installed redis-5.0.4\n"
     ]
    }
   ],
   "source": [
    "# 安装 redis 模块\n",
    "!pip install redis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connected to Redis\n"
     ]
    }
   ],
   "source": [
    "# 连接到Redis\n",
    "import redis\n",
    "\n",
    "# 连接Redis服务器\n",
    "r = redis.Redis(host=\"192.168.1.234\", port=6379, db=14, password=\"3h1admin\")\n",
    "\n",
    "# 测试连接\n",
    "try:\n",
    "    r.ping()\n",
    "    print(\"Connected to Redis\")\n",
    "except redis.ConnectionError as e:\n",
    "    print(\"Failed to connect to Redis. Caused by \", e)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Alice\n"
     ]
    }
   ],
   "source": [
    "# 设置和获取键值对\n",
    "r.set('name', \"Alice\")\n",
    "\n",
    "# 获取键值对\n",
    "name = r.get(\"name\")\n",
    "print(f\"Name: {name.decode('utf-8')}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Temporary Value: This is temporary\n"
     ]
    }
   ],
   "source": [
    "# 设置过期时间的键值对\n",
    "r.set('temp', \"This is temporary\", ex=10)\n",
    "\n",
    "# 获取键值对\n",
    "temp_value = r.get(\"temp\")\n",
    "print(f\"Temporary Value: {temp_value.decode('utf-8')}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Does 'name' exist? Yes\n"
     ]
    }
   ],
   "source": [
    "# 检查键是否存在\n",
    "exists = r.exists('name')\n",
    "print(f\"Does 'name' exist? {'Yes' if exists else 'No'}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name after deletion: None\n"
     ]
    }
   ],
   "source": [
    "# 删除键\n",
    "r.delete('name')\n",
    "\n",
    "name = r.get('name')\n",
    "print(f\"Name after deletion: {name}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User Name: John, Age: 30\n",
      "User: {'name': 'John', 'age': '30'}\n"
     ]
    }
   ],
   "source": [
    "# 操作哈希\n",
    "# 设置哈希值\n",
    "r.hset('user:1000', 'name', 'John')\n",
    "r.hset('user:1000', 'age', 30)\n",
    "\n",
    "# 获取哈希值\n",
    "name = r.hget('user:1000', 'name')\n",
    "age = r.hget('user:1000', 'age')\n",
    "print(f\"User Name: {name.decode('utf-8')}, Age: {age.decode('utf-8')}\")\n",
    "\n",
    "# 获取整个哈希\n",
    "user = r.hgetall('user:1000')\n",
    "user_dict = {k.decode('utf-8'): v.decode('utf-8') for k, v in user.items()}\n",
    "print(f\"User: {user_dict}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fruits: ['cherry', 'banana', 'apple']\n",
      "Left pop: cherry, Right pop: apple\n"
     ]
    }
   ],
   "source": [
    "# 删除列表\n",
    "r.delete('fruits')\n",
    "# 操作列表\n",
    "r.lpush('fruits', 'apple')\n",
    "r.lpush('fruits', 'banana')\n",
    "r.lpush('fruits', 'cherry')\n",
    "\n",
    "# 获取列表中的所有元素\n",
    "fruits = r.lrange('fruits', 0, -1)\n",
    "fruits_list = [fruit.decode('utf-8') for fruit in fruits]\n",
    "print(f\"Fruits: {fruits_list}\")\n",
    "\n",
    "# 弹出列表中的元素\n",
    "left_fruit = r.lpop('fruits')\n",
    "right_fruit = r.rpop('fruits')\n",
    "print(f\"Left pop: {left_fruit.decode('utf-8')}, Right pop: {right_fruit.decode('utf-8')}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rtt min/avg/max/mdev = 25.489/25.807/27.763/0.674 ms\n",
      "\n",
      "10 packets transmitted, 10 received, 0% packet loss, time 9014ms\n",
      "\n",
      "--- baidu.com ping statistics ---\n",
      "\n",
      "\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=10 ttl=49 time=25.5 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=9 ttl=49 time=25.6 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=8 ttl=49 time=25.6 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=7 ttl=49 time=27.7 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=6 ttl=49 time=25.4 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=5 ttl=49 time=25.5 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=4 ttl=49 time=25.6 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=3 ttl=49 time=25.6 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=49 time=25.5 ms\n",
      "\n",
      "64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=49 time=25.5 ms\n",
      "\n",
      "PING baidu.com (39.156.66.10) 56(84) bytes of data.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 获取列表中的所有元素\n",
    "logs = r.lrange('train_task1', 0, -1)\n",
    "log_list = [log.decode('utf-8') for log in logs]\n",
    "for log in log_list:\n",
    "    print(log)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tags: {'python', 'database', 'redis'}\n",
      "Is 'python' a member of 'tags'? Yes\n"
     ]
    }
   ],
   "source": [
    "# 操作集合\n",
    "r.sadd('tags', 'python')\n",
    "r.sadd('tags', 'redis')\n",
    "r.sadd('tags', 'database')\n",
    "r.sadd('tags', 'redis')\n",
    "\n",
    "# 获取集合中的所有元素\n",
    "tags = r.smembers('tags')\n",
    "tags_set = {tag.decode('utf-8') for tag in tags}\n",
    "print(f\"Tags: {tags_set}\")\n",
    "\n",
    "# 检查元素是否在集合中\n",
    "is_member = r.sismember('tags', 'python')\n",
    "print(f\"Is 'python' a member of 'tags'? {'Yes' if is_member else 'No'}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Foo: bar, Baz: qux\n"
     ]
    }
   ],
   "source": [
    "# 使用事务\n",
    "# 使用事务执行一组操作\n",
    "with r.pipeline() as pipe:\n",
    "    pipe.set('foo', 'bar')\n",
    "    pipe.set('baz', 'qux')\n",
    "    results = pipe.execute()\n",
    "\n",
    "# 获取结果\n",
    "foo = r.get('foo')\n",
    "baz = r.get('baz')\n",
    "print(f\"Foo: {foo.decode('utf-8')}, Baz: {baz.decode('utf-8')}\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Received: Message 0\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 0'}\n",
      "Received: Message 0\n",
      "Received: Message 0\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 1'}Received: Message 1\n",
      "Received: Message 1\n",
      "Received: Message 1\n",
      "\n",
      "Received: Message 1\n",
      "Received: Message 2{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 2'}\n",
      "Received: Message 2\n",
      "Received: Message 2\n",
      "\n",
      "Received: Message 2\n",
      "Received: Message 3Received: Message 3\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 3'}\n",
      "Received: Message 3\n",
      "Received: Message 3\n",
      "\n",
      "Received: Message 4{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 4'}\n",
      "Received: Message 4\n",
      "Received: Message 4\n",
      "Received: Message 4\n",
      "\n",
      "Received: Message 5Received: Message 5\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 5'}\n",
      "Received: Message 5\n",
      "Received: Message 5\n",
      "\n",
      "Received: Message 6{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 6'}\n",
      "Received: Message 6\n",
      "Received: Message 6\n",
      "\n",
      "Received: Message 6\n",
      "Received: Message 7\n",
      "Received: Message 7\n",
      "Received: Message 7\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 7'}\n",
      "Received: Message 7\n",
      "Received: Message 8{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 8'}\n",
      "Received: Message 8\n",
      "Received: Message 8\n",
      "Received: Message 8\n",
      "\n",
      "Received: Message 9Received: Message 9\n",
      "{'type': 'message', 'pattern': None, 'channel': b'channel', 'data': b'Message 9'}\n",
      "Received: Message 9\n",
      "Received: Message 9\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#订阅发布功能\n",
    "\n",
    "# 发布者\n",
    "def publisher():\n",
    "    import time\n",
    "    for i in range(10):\n",
    "        r.publish('channel', f\"Message {i}\")\n",
    "        time.sleep(1)\n",
    "\n",
    "# 订阅者\n",
    "def subscriber():\n",
    "    pubsub = r.pubsub()\n",
    "    pubsub.subscribe('channel')\n",
    "    for message in pubsub.listen():\n",
    "        if message['type'] == 'message':\n",
    "            print(f\"Received: {message['data'].decode('utf-8')}\")\n",
    "\n",
    "import threading\n",
    "\n",
    "# 启动发布者和订阅者\n",
    "p1 = threading.Thread(target=publisher)\n",
    "p2 = threading.Thread(target=subscriber)\n",
    "p1.start()\n",
    "p2.start()\n",
    "p1.join()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}